package com.wps.wdztv6.config;


import com.mybatisflex.core.query.QueryWrapper;
import com.wps.wdztv6.api.common.TemplateFileEnum;
import com.wps.wdztv6.entity.TemplateFile;
import com.wps.wdztv6.entity.table.TemplateFileTableDef;
import com.wps.wdztv6.service.FileService;
import com.wps.wdztv6.service.TemplateFileService;
import com.wps.wdztv6.utils.WPS4Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.List;


@Component
@Slf4j
//public class InitApplication implements CommandLineRunner {
public class InitApplication implements CommandLineRunner {

    @Autowired
    private FileService fileService;

    @Autowired
    private TemplateFileService templateFileService;

    @Override
    public void run(String... args) {
        log.info(">>>>>>>>>>>>>>> application init start <<<<<<<<<<<<<");

        log.info("初始化wdztapp信息");
        log.info("host: {}", WdztAppinfoProperties.host);
        WdztAppInfoConfig.host = WdztAppinfoProperties.host;
        log.info("ak: {}", WdztAppinfoProperties.ak);
        WdztAppInfoConfig.ak = WdztAppinfoProperties.ak;
        log.info("sk: {}", WdztAppinfoProperties.sk);
        WdztAppInfoConfig.sk = WdztAppinfoProperties.sk;
        WPS4Util.initAppInfo(WdztAppinfoProperties.ak, WdztAppinfoProperties.sk);

        log.info("myserverUrl: {}", AppServersProperties.myserverUrl);

        //String uploadPath = WdztAppinfoProperties.filePath + File.separator + WdztAppinfoProperties.uploadPath + File.separator;
        log.info("设置的文件上传路径:{}", WdztAppinfoProperties.uploadPath);
        File folder = new File(WdztAppinfoProperties.uploadPath);
        log.info("真实的上传路径:{}", WdztAppinfoProperties.uploadPath());
        if (!folder.isDirectory()) {
            log.info("上传路径不存在，创建");
            boolean result = folder.mkdirs();
            if (result) {
                log.info("上传路径创建成功");
            } else {
                log.error("上传路径创建失败");
                System.exit(500);
            }
        }

        log.info("设置的模板文件保存路径:{}", WdztAppinfoProperties.templateFilePath);
        File templateFilePathFolder = new File(WdztAppinfoProperties.templateFilePath);
        log.info("真实的模板文件保存路径:{}", WdztAppinfoProperties.templateFilePath());
        if (!templateFilePathFolder.isDirectory()) {
            log.info("模板文件保存路径不存在，创建");
            boolean result = templateFilePathFolder.mkdirs();
            if (result) {
                log.info("模板文件保存路径创建成功");
            } else {
                log.error("模板文件保存路径创建失败");
                System.exit(500);
            }
        }

        //wps,0
        log.info("检查基础文件模板:>>>>>");
        QueryWrapper queryWrapper = QueryWrapper.create()
                .and(TemplateFileTableDef.TEMPLATE_FILE.SOURCE.eq("wps"))
                .and(TemplateFileTableDef.TEMPLATE_FILE.CATEGORY.eq("0"))
                .and(TemplateFileTableDef.TEMPLATE_FILE.EFFECT_FLAG.eq(1));
        List<TemplateFile> list = templateFileService.list(queryWrapper);

        if (list.size() >= 6) {
            log.info("存在wps模板文件:{}", list);
        } else {
            log.info("缺失wps的基础模板文件!!!");
            log.info("初始化wps的基础模板文件>>>>>>");

            log.info("初始化Docx的基础模板文件>>>>>>");
            //initTemplateDocx();
            initTemplateBase(TemplateFileEnum.DOCX);

            log.info("初始化Pdf的基础模板文件>>>>>>");
            initTemplateBase(TemplateFileEnum.PDF);

            log.info("初始化Pptx的基础模板文件>>>>>>");
            initTemplateBase(TemplateFileEnum.PPTX);

            log.info("初始化Xlsx的基础模板文件>>>>>>");
            initTemplateBase(TemplateFileEnum.XLSX);


        }

        log.info(">>>>>>>>>>>>>>> application init end <<<<<<<<<<<<<");

    }


    private void initTemplateBase(TemplateFileEnum templateFileEnum) {
        
        QueryWrapper templateFileQueryWrapper = QueryWrapper.create()
                .and(TemplateFileTableDef.TEMPLATE_FILE.CODE.eq(templateFileEnum.getCode()))
                .and(TemplateFileTableDef.TEMPLATE_FILE.SOURCE.eq("wps"))
                .and(TemplateFileTableDef.TEMPLATE_FILE.CATEGORY.eq("0"))
                .and(TemplateFileTableDef.TEMPLATE_FILE.EFFECT_FLAG.eq(1));
        TemplateFile templateFile = templateFileService.getOne(templateFileQueryWrapper);
        if (templateFile == null) {
            try {
                templateFileService.createTemplateBase(templateFileEnum);
            } catch (IOException e) {
                log.error("createTemplate_{}模板文件异常:{}", templateFileEnum.getCode(), e.getMessage());
                System.exit(500);
            }
            log.info("createTemplate_{}模板文件完成", templateFileEnum.getCode());
        } else {
            log.info("createTemplate_{}模板文件已存在:{}", templateFileEnum.getCode(), templateFile);
        }
    }


}